package org.jdns.jfreechess.server.commands;

import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import org.jdns.jfreechess.server.ChessServer;
import org.jdns.jfreechess.server.CommandHandler;
import org.jdns.jfreechess.server.CommandName;
import org.jdns.jfreechess.server.User;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;

@CommandName(names = { "who" })
public class WhoCommand implements Command {
  private final ChessServer server;

  @Inject
  public WhoCommand(ChessServer server) {
    this.server = server;
  }

  @CommandHandler
  public String handleWhoCommand() {
    List<User> sortedUsers = Ordering.natural().onResultOf(new Function<User, String>() {
      @Override
      public String apply(User user) {
        return user.getName().toLowerCase();
      }
    }).immutableSortedCopy(server.getConnectedUsers());

    return Joiner.on("\n").join(Iterables.transform(sortedUsers, User.NAME_FUNCTION));
  }
}
